package com.cius.ssm.service.impl;

import com.cius.ssm.entity.Product;
import com.cius.ssm.entity.ProductAndUser;
import com.cius.ssm.entity.ProductAndUserVo;
import com.cius.ssm.exception.CustomException;
import com.cius.ssm.mapper.ProductAndUserMapper;
import com.cius.ssm.mapper.ProductMapper;
import com.cius.ssm.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Created by ciuscheung on 17/4/4.
 */
public class ProductServiceImpl implements ProductService{

    @Autowired
    private ProductAndUserMapper productAndUserMapper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<ProductAndUser> findProductAndUser(ProductAndUserVo productAndUserVo) throws Exception {
        return productAndUserMapper.findProductAndUser(productAndUserVo);
    }

    @Override
    public Product findProductById(int id) throws Exception {
        Product product = productMapper.selectByPrimaryKey(id);
        //异常处理
        //判断是否为空,根据id没有查到商品,抛出异常,提示用户商品信息不存在
        if (product == null){
            throw new CustomException("修改的商品信息不存在");
        }
        return product;
    }

    @Override
    public void update(ProductAndUser productAndUser) throws Exception {
        productMapper.updateByPrimaryKey(productAndUser);
    }
}
